//
//  SeouYeViewController.m
//  Experss
//
//  Created by 嗯，大葱 on 17/7/27.
//  Copyright © 2017年 嗯，大葱. All rights reserved.
//

#import "SeouYeViewController.h"
#import "Express.pch"
#import "AppDelegate.h"
#import <SDCycleScrollView/SDCycleScrollView.h>

#import "ShouYeTwoCell.h"
#import "ShouYeOneCell.h"
#import "ShouYwThirdCell.h"
#import "ShouYeFourCell.h"
#import "ShouYeFiveCell.h"
#import "ShouYeSixCell.h"
#import "HotShopCollectionView.h"//热门商店
#import "ShouYeAddressController.h"//地址
#import "MyMessageViewController.h"//我的消息
#import "SearchVController.h"//搜索界面

#import "TextFiledAddImage.h"

#import "NoTimBuyController.h"//限时秒杀

#import "ManJianPlayController.h"//满减活动

#import "ShouYeCyCell.h"//首页轮播图

#import "ShouYeFenLeiModle.h"



//标题菜单按钮
#import "DataSource.h"

#import "fenLeiDetailViewController.h"//分类详情


//热门商铺

#import "InStoreController.h"

#import "NearlyShopModel.h"//附近的批发商
#import "NearlyShopModel2.h"

#import<CommonCrypto/CommonDigest.h>

@interface SeouYeViewController () <UITableViewDelegate, UITableViewDataSource, SDCycleScrollViewDelegate,YANScrollMenuProtocol,chooseCollectionIndepathDelegate,UITextFieldDelegate>
{
    
    NSInteger row;
    NSInteger item;
    UITextField *seachField;//搜索框
    
    NSMutableArray *_imagesURLStrings;//轮播图片
    NSArray *_infoLunBoArr;
    
    CGFloat type1;
    int _page;
    UINib *_cellNib;
    
    ////
    NSMutableArray *footPopExistArr;
    NSMutableArray *footPopArr;
    NSMutableDictionary *saveFootContentDic;
    
    int countArray;//请求附近批发商的个数
    
}



@property (nonatomic, strong) YANScrollMenu *menu;
/**
 *  dataSource
 */
@property (nonatomic, strong) NSMutableArray<DataSource *> *dataSource;


@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) SDCycleScrollView *advView;
@property (nonatomic, strong)UIButton *rightBtn;//左侧按钮
@property (nonatomic, strong)UIButton *rightBtn2;//右侧按钮


@property (nonatomic, strong) HotShopCollectionView *hotView;//热门商店

@property (nonatomic, strong) NSMutableArray *fenLeiDataArr;

@property (nonatomic, strong)NSMutableArray *noTimeArray;//用来存放限时秒杀类数据

@property (nonatomic, strong)NSMutableArray *nearlyArray;

@property (nonatomic, strong)NSMutableArray *nearlyArray2;

@end

@implementation SeouYeViewController
- (NSMutableArray<DataSource *> *)dataSource{
    if (_dataSource == nil) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];


  

    
  
    
    
    
    
    
    
    self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];

    
    if (type1 == 0) {
         [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
        [[self.navigationController.navigationBar subviews] objectAtIndex:0].alpha = 0;

    }else{
         [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
        [[self.navigationController.navigationBar subviews] objectAtIndex:0].alpha = 1;

    }
    
    //检测是否有默认地址
    [self cheakIsAddress];
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    
    _page = 1;
    _imagesURLStrings = [NSMutableArray array];
    _infoLunBoArr = [NSArray array];
    
    _fenLeiDataArr = [NSMutableArray array];
    _noTimeArray = [NSMutableArray array];
    _nearlyArray = [NSMutableArray array];
    _nearlyArray2 = [NSMutableArray array];
    [self setupNavItems];
    [self GetBanner];//轮播图
    [self GetCategory];//分类
    [self getNoTimeBuyData];//限时秒杀
    [self getNearblyShopData];//获取附近的批发商
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    [[self.navigationController.navigationBar subviews] objectAtIndex:0].alpha = 0;
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
  
    
    [self.view addSubview:self.tableView];
    
    
    
        
 
    
}

#pragma mark 检测是否有默认地址
//检测是否有默认地址
- (void)cheakIsAddress {
    NSDictionary *saveDic = [SaveDataMethord readLoginData];
    
    
    NSString *userid = saveDic[@"userid"];
   
    NSDictionary *dic = @{@"userid":saveDic[@"userid"],@"actoken":[Md5Apiway getMD5data:userid],@"actoken":[Md5Apiway getMD5data:saveDic[@"userid"]]};

    
    
    
    [HttpRequest postWithURLString:POST(IsDefaultAddress) parameters:dic success:^(id responseObject) {
        if ([responseObject[@"ret"] intValue] != 200) {
            [[DSLHUD shareDSLHUD] showTextNoActivity:@"请求错误" timeLength:1.5];
            return ;
        }
        NSDictionary *tempD = responseObject[@"data"];
        if ([[NSString stringWithFormat:@"%@",tempD[@"code"]] isEqualToString:@"-1"]) {
            
            [[DSLHUD shareDSLHUD] showHUDWithLable:@"请先选择收货地址" SuperView:self.view FontSize:14 HudDismissTiem:2 HudDismissBlock:^{
                ShouYeAddressController *address = [[ShouYeAddressController alloc] init];
                address.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:address animated:YES];
            }];
        }else if([[NSString stringWithFormat:@"%@",tempD[@"code"]] isEqualToString:@"1"]){
            
            
            [HttpRequest getWithURLString:POST(GetDefaultAddress) parameters:dic success:^(id responseObject) {
              
                [  _rightBtn setTitle:responseObject[@"data"][@"info"][@"address"] forState:(UIControlStateNormal)];
                
                
                _page = 1;//页数置为1
          
                [self   getNearblyShopData];
                
                
            } failure:^(NSError *error) {
                
            }];
        }else {
         
            
        }
    } failure:^(NSError *error) {
        
    }];
}

- (void)setupNavItems
{
    
    //设置导航栏按钮的边距
    _rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _rightBtn.frame= CGRectMake(0, 0, 95, 44);
    [_rightBtn setTitle:@"望京大厦B座fdsf fas" forState:(UIControlStateNormal)];
    _rightBtn.titleLabel.font = kFont16;
    
    _rightBtn.titleLabel.lineBreakMode =  NSLineBreakByTruncatingTail;
    _rightBtn.titleLabel.textAlignment = NSTextAlignmentLeft;
    [_rightBtn setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    [_rightBtn addTarget:self action:@selector(onClickLeft) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *btn_left = [[UIBarButtonItem alloc] initWithCustomView:_rightBtn];
    UIBarButtonItem *negativeSpacerleft = [[UIBarButtonItem alloc]   initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace   target:nil action:nil];
    /**
     width为负数时，相当于btn向右移动width数值个像素，由于按钮本身和  边界间距为5pix，所以width设为-5时，间距正好调整为0；width为正数 时，正好相反，相当于往左移动width数值个像素
     */
    negativeSpacerleft.width = -10;
    self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:negativeSpacerleft, btn_left, nil];
    
    
    
 
    
    seachField = [[UITextField alloc] initWithFrame:CGRectMake(0,-3,kScreen_Width - 100, 30)];
    seachField.text = @"烧饼";
    seachField.backgroundColor = [UIColor colorWithRed:241/255.0 green:245/255.0 blue:251/255.0 alpha:0.8];
    seachField.font = [UIFont systemFontOfSize:14];
    seachField.textColor = [UIColor whiteColor];
    seachField.layer.cornerRadius = CGRectGetHeight(seachField.frame)/2;
    seachField.layer.masksToBounds = YES;
    [TextFiledAddImage setLeftViewWithTextField:seachField imageName:@"shouye_sousuo"];

    self.navigationItem.titleView  = seachField;
    
    seachField.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSearch:)];
    [seachField addGestureRecognizer:tap];
    
    
    //设置导航栏按钮的边距
   _rightBtn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    _rightBtn2.frame= CGRectMake(0, 0, 60, 30);
    [_rightBtn2 setImage:[UIImage imageNamed:@"shouye_xiaoxi"] forState:(UIControlStateNormal)];
    [_rightBtn2 addTarget:self action:@selector(onClickRight) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *btn_right = [[UIBarButtonItem alloc] initWithCustomView:_rightBtn2];
    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]   initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace   target:nil action:nil];
    negativeSpacer.width = -20;
    self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:negativeSpacer, btn_right, nil];
    
    
    
 
    
    
    
}

#pragma mark 输入框的轻拍事件
- (void)handleSearch:(UITapGestureRecognizer *)tap {
    SearchVController *address = [[SearchVController alloc] init];
    UINavigationController *naV =[[UINavigationController alloc] initWithRootViewController:address];
    address.hidesBottomBarWhenPushed = YES;
    [self presentViewController:naV animated:YES completion:nil];
}



- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
 
//    type1 = 0;
    
    CGFloat offsetY = scrollView.contentOffset.y;
    type1 = scrollView.contentOffset.y;
    
    if (offsetY < -IMAGE_HEIGHT) {
         //[[self.navigationController.navigationBar subviews] objectAtIndex:0].alpha = self.tableView.contentOffset.y/((860*kScreen_Width)/1242-64);
        self.navigationController.navigationBar.hidden = YES;
        
    } else {
        self.navigationController.navigationBar.hidden = NO;
         [[self.navigationController.navigationBar subviews] objectAtIndex:0].alpha = self.tableView.contentOffset.y/64;
    }
    
    if (offsetY > NAVBAR_COLORCHANGE_POINT)
    {
        CGFloat alpha = (offsetY - NAVBAR_COLORCHANGE_POINT) / NAV_HEIGHT;
       self.navigationController.navigationBar.hidden = NO;
        [self updateSearchBarColor:alpha];
    }
    else
    {
        //self.navigationController.navigationBar.hidden = YES;
        seachField.textColor = [UIColor whiteColor];
        [TextFiledAddImage setLeftViewWithTextField:seachField imageName:@"shouye_sousuo"];
        
        [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
        [_rightBtn setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
        
        [_rightBtn2 setImage:[UIImage imageNamed:@"shouye_xiaoxi"] forState:(UIControlStateNormal)];
    }
    
    if (offsetY == 0) {
        
    }
    //限制下拉的距离
    if(offsetY < LIMIT_OFFSET_Y-30) {
        [scrollView setContentOffset:CGPointMake(0, LIMIT_OFFSET_Y-30)];
    }
}


#pragma mark 向上滑动，导航栏控件的改变
- (void)updateSearchBarColor:(CGFloat)alpha
{
 
    seachField.textColor = [UIColor lightGrayColor];
    [TextFiledAddImage setLeftViewWithTextField:seachField imageName:@"fenlei_sousuo"];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;

    [_rightBtn setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
     [_rightBtn2 setImage:[UIImage imageNamed:@"fenlei_xiaoxi"] forState:(UIControlStateNormal)];
}

#pragma mark - tableview delegate / dataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 7;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return kScreen_Height/667*230;
    }
    if (indexPath.section == 1) {
        return 110;
    }
    if (indexPath.section == 3 || indexPath.section == 5) {
        return  kScreen_Height/667*60;
    }
    if (indexPath.section == 6) {
        return 203;
    }
    
    if (indexPath.section ==2) {
        return    kScreen_Height/667 *200;
    }
  
    else {
    return 200;
    }
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 6) {
        return _nearlyArray.count;
    }
    else {
    return 1;
    }
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 0) {
        ShouYeCyCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ShouYeCyCell"];
        if (_advView) {
            [_advView removeFromSuperview];
        }
        
        _advView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0,0, kScreenWidth, kScreen_Height/667*230)delegate:self placeholderImage:[UIImage imageNamed:@"placeholder"]];
        
        NSArray *imagesURLStrings = [NSArray arrayWithArray:_imagesURLStrings];//网络图片
        // 网络数据
        _advView.imageURLStringsGroup = imagesURLStrings;
        
        _advView.pageDotColor = [UIColor grayColor];
        _advView.autoScrollTimeInterval = 5;
        _advView.currentPageDotColor = [UIColor whiteColor];
        _advView.pageDotImage = [UIImage imageNamed:@"未标题-1"];
        _advView.currentPageDotImage = [UIImage imageNamed:@"shouye_lunbo1"];
        _advView.pageControlDotSize = CGSizeMake(10, 10);
       
        _advView.bannerImageViewContentMode = UIViewContentModeScaleAspectFill;
        [cell.contentView addSubview:_advView];
        return cell;
}
    else if (indexPath.section == 1) {
        ShouYeOneCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ShouYeOneCell"];
        cell.selectionStyle = kSeNone;
        self.menu = [[YANScrollMenu alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 70)];
        self.menu.backgroundColor = [UIColor whiteColor];
        self.menu.frame = CGRectMake(0, 0, kScreen_Width, 100);
        
        self.menu.delegate = self;
        [cell.contentView addSubview:self.menu];
        
        [YANMenuItem appearance].textFont = [UIFont systemFontOfSize:12];
        [YANMenuItem appearance].textColor = [UIColor colorWithRed:51/255.f green:51/255.f blue:51/255.f alpha:1.0];
        return cell;
    }else if (indexPath.section == 2){
        ShouYeTwoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ShouYeTwoCell"];
        cell.selectionStyle = kSeNone;
        cell.oneImage.userInteractionEnabled = YES;
        cell.twoImage.userInteractionEnabled = YES;
        cell.thridImage.userInteractionEnabled = YES;
        cell.fourImage.userInteractionEnabled = YES;
        cell.fiveImage.userInteractionEnabled = YES;
        UITapGestureRecognizer *tapOne = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapImage:)];
        [cell.oneImage addGestureRecognizer:tapOne];
        
        UITapGestureRecognizer *tapTwo = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapImage222:)];
        [cell.twoImage addGestureRecognizer:tapTwo];
        
        UITapGestureRecognizer *tapThird = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapImage222:)];
        [cell.thridImage addGestureRecognizer:tapThird];
        
        UITapGestureRecognizer *tapFour = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapImage222:)];
        [cell.fourImage addGestureRecognizer:tapFour];
        
        
        UITapGestureRecognizer *tapFive = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapImage222:)];
        [cell.fiveImage addGestureRecognizer:tapFive];
        
        if (_noTimeArray.count == 0) {
            
        }
       else if (_noTimeArray.count ==1) {
              [cell.oneImage sd_setImageWithURL:[NSURL URLWithString:_noTimeArray[0][@"image"]]placeholderImage:[UIImage imageNamed:@"quetu"]];
        }
      else  if (_noTimeArray.count ==2) {
            [cell.oneImage sd_setImageWithURL:[NSURL URLWithString:_noTimeArray[0][@"image"]]placeholderImage:[UIImage imageNamed:@"quetu"]];
          
            [cell.twoImage sd_setImageWithURL:[NSURL URLWithString:_noTimeArray[1][@"image"]]placeholderImage:[UIImage imageNamed:@"quetu"]];
          cell.twoImage.tag = 101+[_noTimeArray[1][@"id"] intValue];
        }
      else  if (_noTimeArray.count ==3) {
          [cell.oneImage sd_setImageWithURL:[NSURL URLWithString:_noTimeArray[0][@"image"]]placeholderImage:[UIImage imageNamed:@"quetu"]];
          [cell.twoImage sd_setImageWithURL:[NSURL URLWithString:_noTimeArray[1][@"image"]]placeholderImage:[UIImage imageNamed:@"quetu"]];
          cell.twoImage.tag = 101+[_noTimeArray[1][@"id"] intValue];
          [cell.thridImage sd_setImageWithURL:[NSURL URLWithString:_noTimeArray[2][@"image"]]placeholderImage:[UIImage imageNamed:@"quetu"]];
          cell.thridImage.tag = 101+[_noTimeArray[2][@"id"] intValue];

      }
      else  if (_noTimeArray.count ==4) {
          [cell.oneImage sd_setImageWithURL:[NSURL URLWithString:_noTimeArray[0][@"image"]]placeholderImage:[UIImage imageNamed:@"quetu"]];
          [cell.twoImage sd_setImageWithURL:[NSURL URLWithString:_noTimeArray[1][@"image"]]placeholderImage:[UIImage imageNamed:@"quetu"]];
          [cell.thridImage sd_setImageWithURL:[NSURL URLWithString:_noTimeArray[2][@"image"]]placeholderImage:[UIImage imageNamed:@"quetu"]];
          [cell.fourImage sd_setImageWithURL:[NSURL URLWithString:_noTimeArray[3][@"image"]]placeholderImage:[UIImage imageNamed:@"quetu"]];
          
          cell.twoImage.tag = 101+[_noTimeArray[1][@"id"] intValue];
          cell.thridImage.tag = 101+[_noTimeArray[2][@"id"] intValue];
          cell.fourImage.tag = 101+[_noTimeArray[3][@"id"] intValue];
          
      }
        
        else {
             [cell.oneImage sd_setImageWithURL:[NSURL URLWithString:_noTimeArray[0][@"image"]]placeholderImage:[UIImage imageNamed:@"quetu"]];
              [cell.twoImage sd_setImageWithURL:[NSURL URLWithString:_noTimeArray[1][@"image"]]placeholderImage:[UIImage imageNamed:@"quetu"]];
              [cell.thridImage sd_setImageWithURL:[NSURL URLWithString:_noTimeArray[2][@"image"]]placeholderImage:[UIImage imageNamed:@"quetu"]];
              [cell.fourImage sd_setImageWithURL:[NSURL URLWithString:_noTimeArray[3][@"image"]]placeholderImage:[UIImage imageNamed:@"quetu"]];
              [cell.fiveImage sd_setImageWithURL:[NSURL URLWithString:_noTimeArray[4][@"image"]]placeholderImage:[UIImage imageNamed:@"quetu"]];
            cell.twoImage.tag = 101+[_noTimeArray[1][@"id"] intValue];
            cell.thridImage.tag = 101+[_noTimeArray[2][@"id"] intValue];
            cell.fourImage.tag = 101+[_noTimeArray[3][@"id"] intValue];
            cell.fiveImage.tag = 101+[_noTimeArray[4][@"id"] intValue];
        
        }
        return cell;
    }
    
    else if(indexPath.section == 3){
        ShouYwThirdCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ShouYwThirdCell"];
        cell.selectionStyle = kSeNone;
        return cell;
    }else if(indexPath.section == 4){
        ShouYeFourCell *cell  = [tableView dequeueReusableCellWithIdentifier:@"ShouYeFourCell"];
        cell.selectionStyle = kSeNone;
        if (_hotView) {
            [_hotView removeFromSuperview];
        }
        _hotView = [[HotShopCollectionView alloc] initWithFrame:CGRectMake(0, 0, kScreen_Width, 200)];
        _hotView.delegate = self;
        [cell.contentView addSubview:_hotView];
        return cell;
    }else if (indexPath.section == 5){
        ShouYeFiveCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ShouYeFiveCell"];
        cell.selectionStyle = kSeNone;

        return cell;
    }else if(indexPath.section == 6){
        
        ShouYeSixCell *  cell = [_cellNib instantiateWithOwner:nil options:nil][0];
     
        cell.selectionStyle = kSeNone;
        NearlyShopModel *modle = self.nearlyArray[indexPath.row];
        NSLog(@"--=-=-=%@",modle.shop_name);
        [cell.logoV sd_setImageWithURL:[NSURL URLWithString:modle.logo] placeholderImage:[UIImage imageNamed:@"quetu"]];
        cell.nameLB.text = modle.shop_name;
        
        if (modle.goods.count == 0) {
            
        }else if (modle.goods.count == 1) {
            NearlyShopModel2 *modl2 = modle.goods[0];
            cell.oneLB.text = modl2.name;
            [cell.oneImageV sd_setImageWithURL:[NSURL URLWithString:modl2.image]];
            if ([modl2.is_hot isEqualToString:@"1"]) {
                cell.hotOneLB.hidden = NO;
            }else {
                cell.hotOneLB.hidden = YES;
            }
            
        }
        
        else if (modle.goods.count == 2) {
            NearlyShopModel2 *modl2 = modle.goods[0];
            cell.oneLB.text = modl2.name;
            [cell.oneImageV sd_setImageWithURL:[NSURL URLWithString:modl2.image]];
            
            
            
            NearlyShopModel2 *modl3 = modle.goods[1];
            cell.twoLB.text = modl3.name;
            [cell.twoImageV sd_setImageWithURL:[NSURL URLWithString:modl3.image]];
            if ([modl2.is_hot isEqualToString:@"1"]) {
                cell.hotOneLB.hidden = NO;
            }else {
                cell.hotOneLB.hidden = YES;
            }
            if ([modl3.is_hot isEqualToString:@"1"]) {
                cell.hotOneLB.hidden = NO;
            }else {
                cell.hotOneLB.hidden = YES;
            }
            
            
            
            
        }
        
        else if (modle.goods.count == 3) {
            NearlyShopModel2 *modl2 = modle.goods[0];
            cell.oneLB.text = modl2.name;
            [cell.oneImageV sd_setImageWithURL:[NSURL URLWithString:modl2.image]];
            
            NearlyShopModel2 *modl3 = modle.goods[1];
            cell.twoLB.text = modl3.name;
            [cell.twoImageV sd_setImageWithURL:[NSURL URLWithString:modl3.image]];
            
            
            NearlyShopModel2 *modl4 = modle.goods[2];
            cell.thirdLB.text = modl4.name;
            [cell.threeImage sd_setImageWithURL:[NSURL URLWithString:modl4.image]];
            
            
            if ([modl2.is_hot isEqualToString:@"1"]) {
                cell.hotOneLB.hidden = NO;
            }else {
                cell.hotOneLB.hidden = YES;
            }
            if ([modl3.is_hot isEqualToString:@"1"]) {
                cell.hotOneLB.hidden = NO;
            }else {
                cell.hotOneLB.hidden = YES;
            }
            if ([modl4.is_hot isEqualToString:@"1"]) {
                cell.hotOneLB.hidden = NO;
            }else {
                cell.hotOneLB.hidden = YES;
            }
            
            
        }
        
        else if (modle.goods.count == 4) {
            NearlyShopModel2 *modl2 = modle.goods[0];
            cell.oneLB.text = modl2.name;
            [cell.oneImageV sd_setImageWithURL:[NSURL URLWithString:modl2.image]];
            
            NearlyShopModel2 *modl3 = modle.goods[1];
            cell.twoLB.text = modl3.name;
            [cell.twoImageV sd_setImageWithURL:[NSURL URLWithString:modl3.image]];
            
            
            NearlyShopModel2 *modl4 = modle.goods[2];
            cell.thirdLB.text = modl4.name;
            [cell.threeImage sd_setImageWithURL:[NSURL URLWithString:modl4.image]];
          
            
            NearlyShopModel2 *modl5 = modle.goods[3];
            cell.fourLB.text = modl5.name;
            [cell.fourimageV sd_setImageWithURL:[NSURL URLWithString:modl5.image]];
            
            
            
            if ([modl2.is_hot isEqualToString:@"1"]) {
                cell.hotOneLB.hidden = NO;
            }else {
                cell.hotOneLB.hidden = YES;
            }
            if ([modl3.is_hot isEqualToString:@"1"]) {
                cell.hotOneLB.hidden = NO;
            }else {
                cell.hotOneLB.hidden = YES;
            }
            if ([modl4.is_hot isEqualToString:@"1"]) {
                cell.hotOneLB.hidden = NO;
            }else {
                cell.hotOneLB.hidden = YES;
            }
            if ([modl5.is_hot isEqualToString:@"1"]) {
                cell.hotOneLB.hidden = NO;
            }else {
                cell.hotOneLB.hidden = YES;
            }
        }
        
        
        
        
        return cell;
        
    }else {
        return nil;
    }
    
    
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section == 6) {
        
        NearlyShopModel *modle = self.nearlyArray[indexPath.row];
        InStoreController *store = [[InStoreController alloc] init];
        store.shopID = modle.m_id;
        store.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:store animated:YES];
    }
    
}

#pragma mark - getter / setter-=--=-=-==-=-=-=-=-=
- (UITableView *)tableView
{
    if (_tableView == nil) {
        CGRect frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight-44);
        _tableView = [[UITableView alloc] initWithFrame:frame
                                                  style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = kSpNone;
        [_tableView registerNib:[UINib nibWithNibName:@"ShouYeOneCell" bundle:nil] forCellReuseIdentifier:@"ShouYeOneCell"];
        [_tableView registerNib:[UINib nibWithNibName:@"ShouYeTwoCell" bundle:nil] forCellReuseIdentifier:@"ShouYeTwoCell"];
        [_tableView registerNib:[UINib nibWithNibName:@"ShouYwThirdCell" bundle:nil] forCellReuseIdentifier:@"ShouYwThirdCell"];
        [_tableView registerNib:[UINib nibWithNibName:@"ShouYeFourCell" bundle:nil] forCellReuseIdentifier:@"ShouYeFourCell"];
        [_tableView registerNib:[UINib nibWithNibName:@"ShouYeFiveCell" bundle:nil] forCellReuseIdentifier:@"ShouYeFiveCell"];
         [_tableView registerNib:[UINib nibWithNibName:@"ShouYeSixCell" bundle:nil] forCellReuseIdentifier:@"ShouYeSixCell"];
         [_tableView registerNib:[UINib nibWithNibName:@"ShouYeCyCell" bundle:nil] forCellReuseIdentifier:@"ShouYeCyCell"];
        _tableView.backgroundColor = KShouYeBg;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.showsVerticalScrollIndicator = NO;
        // 设置回调（一旦进入刷新状态，就调用target的action，也就是调用self的loadNewData方法）
        
        _cellNib = [UINib nibWithNibName:@"ShouYeSixCell" bundle:nil];
        
        
      /*
        _footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreen_Width,kScreen_Height/667* 60)];
        UIImageView *img = [[UIImageView alloc] init];
        img.contentMode = UIViewContentModeScaleToFill;
        img.frame = CGRectMake(10 , 0,kScreenWidth-20, kScreen_Height/667*60);
        img.image = [UIImage imageNamed:@"daodi"];
        [_footerView addSubview:img];
        self.tableView.tableFooterView = _footerView;
        
        */
        
        MJRefreshGifHeader *headNew = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(requestRefresh)];
        NSMutableArray *refreshingImages2 = [NSMutableArray array];
        for (NSUInteger i =1; i<=12; i++) {
            UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%ld",i]];
            [refreshingImages2 addObject:image];
        }
        
        NSMutableArray *idleImages2 = [NSMutableArray array];
        for (NSUInteger i =1; i<=12; i++) {
            UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%ld",i]];
            [idleImages2 addObject:image];
        }
        // 设置普通状态的动画图片
        [headNew setImages:idleImages2 forState:MJRefreshStateIdle];
        // 设置即将刷新状态的动画图片（一松开就会刷新的状态）
        [headNew setImages:refreshingImages2 forState:MJRefreshStatePulling];
        // 设置正在刷新状态的动画图片
        [headNew setImages:refreshingImages2 duration:2 forState:MJRefreshStateRefreshing];
        // 隐藏状态
        headNew.stateLabel.hidden = YES;
        headNew.lastUpdatedTimeLabel.hidden = YES;
        
        self.tableView.mj_header = headNew;
        
        
        //获取tableView的下拉子视图，在子视图上添加一个背景图片
        UIImageView *imageV = [[UIImageView alloc] init];
        imageV.frame = CGRectMake(0, -50, kScreen_Width, 105);
        imageV.image = [UIImage imageNamed:@"jiazai_di"];
        UIView *springview = self.tableView.subviews.firstObject;
        [springview insertSubview:imageV atIndex:0];
        
        
        
        
        
       _header = [MJRefreshBackGifFooter footerWithRefreshingTarget:self refreshingAction:@selector(requestDta2)];
        NSMutableArray *refreshingImages = [NSMutableArray array];
        for (NSUInteger i =1; i<=12; i++) {
              UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%ld",i]];
            [refreshingImages addObject:image];
        }
        
        NSMutableArray *idleImages = [NSMutableArray array];
        for (NSUInteger i =1; i<=12; i++) {
                UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%ld",i]];
            [idleImages addObject:image];
        }
        // 设置普通状态的动画图片
        [_header setImages:idleImages forState:MJRefreshStateIdle];
        // 设置即将刷新状态的动画图片（一松开就会刷新的状态）
        [_header setImages:refreshingImages forState:MJRefreshStatePulling];
        // 设置正在刷新状态的动画图片
        [_header setImages:refreshingImages duration:2 forState:MJRefreshStateRefreshing];
        // 隐藏状态
        _header.stateLabel.hidden = YES;
        self.tableView.mj_footer = _header;
        
    }
    return _tableView;
}

- (void)requestRefresh {
    _page = 1;
    [self getNearblyShopData];
}

- (void)requestDta2{
        _page++;
        [self getNearblyShopData];
}
- (UIImage *)imageWithImageSimple:(UIImage *)image scaledToSize:(CGSize)newSize
{
    UIGraphicsBeginImageContext(CGSizeMake(newSize.width*2, newSize.height*2));
    [image drawInRect:CGRectMake (0, 0, newSize.width*2, newSize.height*2)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}



#pragma mark 导航栏左侧按钮点击事件
//导航栏左侧按钮点击事件
- (void)onClickLeft
{
    
    ShouYeAddressController *address = [[ShouYeAddressController alloc] init];
    address.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:address animated:YES];
    
    
}
#pragma mark 导航栏右侧按钮点击事件
- (void)onClickRight
{
    MyMessageViewController *address = [[MyMessageViewController alloc] init];
    UINavigationController *naV =[[UINavigationController alloc] initWithRootViewController:address];
    address.hidesBottomBarWhenPushed = YES;
    [self presentViewController:naV animated:YES completion:nil];
}
#pragma mark 导航栏搜索按钮点击事件
- (void)onClickSearchBtn
{
    

}
#pragma  mark 初始化导航栏

#pragma mark - YANScrollMenuProtocol
- (NSUInteger)numberOfRowsForEachPageInScrollMenu:(YANScrollMenu *)scrollMenu{
    
    return 1;
}
- (NSUInteger)numberOfItemsForEachRowInScrollMenu:(YANScrollMenu *)scrollMenu{
    
    return 5;
}
- (NSUInteger)numberOfMenusInScrollMenu:(YANScrollMenu *)scrollMenu{
    
    return self.dataSource.count;
}
- (id<YANMenuObject>)scrollMenu:(YANScrollMenu *)scrollMenu objectAtIndexPath:(NSIndexPath *)indexPath{
    
    NSUInteger idx = indexPath.section * 5 + indexPath.row;
    
    return self.dataSource[idx];
}

- (void)scrollMenu:(YANScrollMenu *)scrollMenu didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    NSUInteger idx = indexPath.section * 5 + indexPath.row;
    fenLeiDetailViewController *vc = [[fenLeiDetailViewController alloc] init];
    vc.cat_id = self.dataSource[idx].Id;
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
    
}
- (YANEdgeInsets)edgeInsetsOfItemInScrollMenu:(YANScrollMenu *)scrollMenu{
    
    return YANEdgeInsetsMake(kScale(10), 0, kScale(5), 0, kScale(5));
}


#pragma mark 热门商场item的点击事件
//点击itme
-(void)chooseCollectionIndepath:(NSIndexPath *)indepath {
    NSLog(@"我是第%ld个热门商场",indepath.row);
}

//点击热门商铺按妞的点击事件
-(void)intoHotShopBtnAction:(UIButton *)sender shopId:(NSString *)shopId  {
    NSLog(@"-------%@",shopId);
    InStoreController *store = [[InStoreController alloc] init];
    store.hidesBottomBarWhenPushed = YES;
    store.shopID = shopId;
    [self.navigationController pushViewController:store animated:YES];
}


- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    [[self.navigationController.navigationBar subviews] objectAtIndex:0].alpha = 1;

}

#pragma mark 限时秒杀以及满减活动的图片轻拍事件
- (void)handleTapImage:(UITapGestureRecognizer *)tap {
    NoTimBuyController *notT = [[NoTimBuyController alloc] init];
    notT.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:notT animated:YES];
    
}
- (void)handleTapImage222:(UITapGestureRecognizer *)tap {
    
    ManJianPlayController *man = [[ManJianPlayController alloc] init];
    man.hidesBottomBarWhenPushed = YES;
    man.manJianId =[NSString stringWithFormat:@"%ld",tap.view.tag -101];
    NSString *strT;
    for (NSDictionary *dic in _noTimeArray) {
        if ([dic[@"id"] isEqualToString:[NSString stringWithFormat:@"%ld",tap.view.tag -101]]) {
            strT = dic[@"name"];
        }
    }
    
    man.titleStr =strT;
    [self.navigationController pushViewController:man animated:YES];
}


#pragma mark －－＝－＝获取首页轮播图图片＝－－－－－
//获取轮播图
-(void)GetBanner{

    NSDictionary *saveDic = [SaveDataMethord readLoginData];
    NSDictionary *parameDic = @{@"userid":saveDic[@"userid"],@"actoken":[Md5Apiway getMD5data:saveDic[@"userid"]]};
    
    [HttpRequest postWithURLString:POST(Shop_GetBanner) parameters:parameDic success:^(id responseObject) {
        if ([responseObject[@"ret"] intValue] != 200) {
            [[DSLHUD shareDSLHUD] showTextNoActivity:@"获取轮播图出错" timeLength:1.5];
            return ;
        }
        NSDictionary *dataDic = [responseObject objectForKey:@"data"];
        _infoLunBoArr = [NSArray arrayWithArray:[dataDic objectForKey:@"info"]];
        
        for (NSDictionary *tempDic in _infoLunBoArr) {
            
            NSString *desc = [tempDic objectForKey:@"desc"];
            [_imagesURLStrings addObject:desc];
        }
        
        NSIndexPath *indexP = [NSIndexPath indexPathForRow:0 inSection:0];
        NSArray *indexpArr = [NSArray arrayWithObject:indexP];
        [self.tableView reloadRowsAtIndexPaths:indexpArr withRowAnimation:(UITableViewRowAnimationNone)];
        
    } failure:^(NSError *error) {
        
    }];
    
}
#pragma mark -------------  首页分类  ---------

//http://bs.m0571.com/mobileapi/public/baishi/?service=Shop_Home.GetHomeCategory&userid=12

-(void)GetCategory{
    NSDictionary *saveDic = [SaveDataMethord readLoginData];
    NSDictionary *parameDic = @{@"userid":saveDic[@"userid"],@"actoken":[Md5Apiway getMD5data:saveDic[@"userid"]]};
    
    [HttpRequest postWithURLString:POST(Shop_GetCategory) parameters:parameDic success:^(id responseObject) {

        if ([responseObject[@"ret"] intValue] != 200) {
            [[DSLHUD shareDSLHUD] showTextNoActivity:@"获取分类出错" timeLength:1.5];
            return ;
        }
        NSDictionary *dataDic = [responseObject objectForKey:@"data"];
        
        if ([[responseObject objectForKey:@"ret"] integerValue] == 200) {
            NSArray *infoArr = [dataDic objectForKey:@"info"];
            
            for (NSUInteger idx = 0; idx< infoArr.count; idx ++) {
                DataSource *object = [[DataSource alloc] init];
                object.text = [infoArr[idx] objectForKey:@"name"];
                object.image = [infoArr[idx] objectForKey:@"image"];
                object.Id = [infoArr[idx] objectForKey:@"id"];
                object.placeholderImage = IMG(@"shouye_jiajuyongp");
                [self.dataSource addObject:object];
            }

        }else{
            [MBProgressHUD showText:@"请求错误"];
        }
        
        [self.tableView reloadData];
        
    } failure:^(NSError *error) {
        
    }];
}


#pragma mark 获取限时秒杀类数据
- (void)getNoTimeBuyData {
    NSDictionary *saveDic = [SaveDataMethord readLoginData];
    
    NSDictionary *parameDic = @{@"userid":saveDic[@"userid"],@"actoken":[Md5Apiway getMD5data:saveDic[@"userid"]]};
    [HttpRequest getWithURLString:POST(Shop_HomeGetHomePromotion) parameters:parameDic success:^(id responseObject) {
        if ([responseObject[@"ret"] intValue] != 200) {
            [[DSLHUD shareDSLHUD] showTextNoActivity:@"获取限时秒杀出错" timeLength:1.5];
            return ;
        }
        
        NSDictionary *tempDic = responseObject[@"data"];
        NSArray *oneArray = tempDic[@"groupbuy"];
     
        for (NSDictionary *tempd in oneArray) {
            //把groupbuy的数组里面的字典取出来存到指定的数组里，最左边的大图片
            [_noTimeArray addObject:tempd];
        }
        NSArray *twoArray = tempDic[@"promotion"];//把右边四个小图片也取出来添加到指定的数组里
        for (NSDictionary *td in twoArray) {
            [_noTimeArray addObject:td];
        }
        
        NSIndexPath *indexP = [NSIndexPath indexPathForRow:0 inSection:2];
        NSArray *indexpArr = [NSArray arrayWithObject:indexP];
        [self.tableView reloadRowsAtIndexPaths:indexpArr withRowAnimation:(UITableViewRowAnimationNone)];
    } failure:^(NSError *error) {
        
    }];
    
}


#pragma mark －＝－＝－＝－＝获取附近的批发商－＝－＝－＝－＝
- (void)getNearblyShopData {
    NSDictionary *saveDic = [SaveDataMethord readLoginData];
    NSDictionary *parameDic = @{@"userid":saveDic[@"userid"],@"page":[NSString stringWithFormat:@"%d",_page],@"limit":@"2",@"actoken":[Md5Apiway getMD5data:saveDic[@"userid"]]};
  
    if (_page  == 1) {
        [_nearlyArray removeAllObjects];
    }
    
    
    [HttpRequest getWithURLString:POST(Shop_HomeGetHomeNearbyShop) parameters:parameDic success:^(id responseObject) {
          [self.tableView.mj_footer endRefreshing];
        [self.tableView.mj_header endRefreshing];
        if ([responseObject[@"ret"] intValue] != 200) {
            [MBProgressHUD showText:@"获取附近批发商出错"];
            return ;
        }
     
        /*
        if ([[NSString stringWithFormat:@"%@",responseObject[@"data"][@"code"]] isEqualToString:@"-2"]) {
            [MBProgressHUD showText:responseObject[@"data"][@"msg"]];
            return;
        }
        */
        
     
        
       
        
        _nearlyArray2 =[NearlyShopModel mj_objectArrayWithKeyValuesArray:[[responseObject objectForKey:@"data"] objectForKey:@"info"]];
        
        [_nearlyArray addObjectsFromArray:_nearlyArray2.mutableCopy];
        
        [self.tableView reloadData];
        if (_nearlyArray2.count == 0) {
            _header.hidden = YES;
            self.tableView.tableFooterView.hidden = NO;
        }else {
            
            _header.hidden = NO;
     
    
            self.tableView.tableFooterView.hidden = YES;
            
        }
    } failure:^(NSError *error) {
        
    }];
}


@end




